package com.quanyan.stadium.biz.impl;

import com.quanyan.stadium.biz.BizPointsDao;
import com.quanyan.stadium.entity.TbPoints;
import com.quanyan.stadium.entity.TbPointsExample;
import com.quanyan.stadium.mapper.TbPointsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by lei.w on 16/6/23.
 */
@Service
public class BizPointsDaoImpl implements BizPointsDao {

    @Autowired
    TbPointsMapper tbPointsMapper;

    @Override
    public List<TbPoints> queryUserPointsBYUserIds(List<Integer> userIdList, Integer categoryId) {

        TbPointsExample tbPointsExample = new TbPointsExample();
        TbPointsExample.Criteria criteria = tbPointsExample.createCriteria();
        criteria.andUidIn(userIdList);
        criteria.andCategoryIdEqualTo(categoryId);

        return tbPointsMapper.selectByExample(tbPointsExample);
    }

    @Override
    public TbPoints getPointsByCategoryIdAndUserIds(Integer uid, Integer categoryId) {
        TbPointsExample tbPointsExample = new TbPointsExample();
        TbPointsExample.Criteria criteria = tbPointsExample.createCriteria();
        criteria.andCategoryIdEqualTo(categoryId);
        criteria.andUidEqualTo(uid);

        List<TbPoints> tbPointsList = tbPointsMapper.selectByExample(tbPointsExample);

        TbPoints points = null;
        if (!(tbPointsList == null || tbPointsList.size() <= 0)) {
            points = tbPointsList.get(0);
        }
        return points;

    }

    @Override
    public int updateUserPoints(TbPoints tbPoints) {

        // 设置修改时间
        tbPoints.setUpdateTime(new Date());

        TbPointsExample tbPointsExample = new TbPointsExample();
        TbPointsExample.Criteria criteria = tbPointsExample.createCriteria();
        criteria.andUidEqualTo(tbPoints.getUid());
        criteria.andCategoryIdEqualTo(tbPoints.getCategoryId());

        return tbPointsMapper.updateByExampleSelective(tbPoints,tbPointsExample);
    }

    @Override
    public int addUserPoints(TbPoints tbPoints) {
        return tbPointsMapper.insertSelective(tbPoints);
    }
}